package com.group.project.hrms;

import com.group.project.hrms.utils.DingAccessUtils;
import com.group.project.hrms.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import tk.mybatis.spring.annotation.MapperScan;

import java.util.Date;

@Slf4j
@SpringBootApplication
@MapperScan("com.group.project.hrms.mapper")
@ServletComponentScan(basePackages = "com.group.project.hrms")
@EnableScheduling
public class HrmsApplication {

	@Autowired
	private DingAccessUtils dingAccessUtils;

	@Autowired
	private LogUtil logUtil;

	public static void main(String[] args) {
		SpringApplication.run(HrmsApplication.class, args);
	}


	/**
	 * 定时执行任务，在用户设置了钉钉小程序认证后，每2小时获取一次Token
	 */
	@Scheduled(initialDelay = 1000, fixedDelay = 7200000)
	public void initialDelay() {
		String token = dingAccessUtils.getAppAccessToken();
		Date date = new Date();
		if(token.equals("N") || token.equals("F")){
			log.info("未配置小程序认证或服务器时间不正确");
		}else {
			log.info("程序自动在 " + date.toLocaleString() + " 获取的Token为: " + token);
			logUtil.insertSystemEventLog("HRMS", date,"自动获取的Token为: " + token, "系统");
		}
	}

	/**
	 * 系统另存在二个个定时器任务
	 * 一个是doAutoSyncDingClockRecord，自动按每3小时同步钉钉打卡记录了（按通讯录所有在职人员） 10800000 = 3小时，存在于SupervisorWebController中
	 * 一个是autoSyncContactsFromDing，自动按每24小时同步通讯录，存在于ContactBasicController中
	 */

}
